From 17dc427ccaa06a17d7ea6e0730592fc3fdec5365 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 9 Feb 2015 08:28:36 -0800 Subject: [PATCH] Improve error message for missing example/bin Closes rust-lang/crates.io#130 --- src/cargo/ops/cargo_run.rs | 11 ++++++++++- tests/test_cargo_test.rs | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_run.rs b/src/cargo/ops/cargo_run.rs index 0ef3986a9..6344ce4fb 100644 --- a/src/cargo/ops/cargo_run.rs +++ b/src/cargo/ops/cargo_run.rs @@ -27,7 +27,16 @@ pub fn run(manifest_path: &Path, !a.get_profile().is_custom_build() }); let bin = try!(bins.next().chain_error(|| { - human("a bin target must be available for `cargo run`") + match (name.as_ref(), &target_kind) { + (Some(name), &TargetKind::Bin) => { + human(format!("no bin target named `{}` to run", name)) + } + (Some(name), &TargetKind::Example) => { + human(format!("no example target named `{}` to run", name)) + } + (Some(_), &TargetKind::Lib(..)) => unreachable!(), + (None, _) => human("a bin target must be available for `cargo run`"), + } })); match bins.next() { Some(..) => return Err( diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index e9279d8eb..fba5d46d8 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -1332,3 +1332,23 @@ test!(bin_is_preserved { execs().with_status(0)); assert_that(&p.bin("foo"), existing_file()); }); + +test!(bad_example { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", ""); + + assert_that(p.cargo_process("run").arg("--example").arg("foo"), + execs().with_status(101).with_stderr("\ +no example target named `foo` to run +")); + assert_that(p.cargo_process("run").arg("--bin").arg("foo"), + execs().with_status(101).with_stderr("\ +no bin target named `foo` to run +")); +}); -- 2.30.2